﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KL.Store.Core.Domain;
using TN.Data;
using TN.Web.Utils;

namespace KL.Store.Data
{
    public class dbAccount
    {
        #region Get

        public static Account Get(string Email)
        {
            IList<Account> objectList = new List<Account>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_UserInfo", objectList, new object[] { Email });

            return objectList.FirstOrDefault();
        }

        #endregion

        #region GetById

        public static Account GetById(int id)
        {
            IList<Account> objectList = new List<Account>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_UserInfoById", objectList, new object[] { id });

            return objectList.FirstOrDefault();
        }

        #endregion

        #region Insert

        public static int Insert(string FirstName, string LastName, string Email, DateTime BirthDay, string Address1, string Address2, string Address3, string Address4,
                                 short City, string Phone, string Mobile, string Fax, string PhoneInternational, string Password, int NewsLetter, int CampaingSmsAndEmail, DateTime Time, string utmRefferal)
        {
            if (!string.IsNullOrEmpty(FirstName)) FirstName = FirstName.Trim();
            if (!string.IsNullOrEmpty(LastName)) LastName = LastName.Trim();
            if (!string.IsNullOrEmpty(Email)) Email = Email.Trim();

            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_UserInfo_Insert", new object[] { FirstName, LastName, Email, BirthDay, Address1, Address2, Address3, Address4, City, Phone, Mobile, Fax, PhoneInternational, Crypt.CreatePasswordHash(Password), NewsLetter, CampaingSmsAndEmail, Time, utmRefferal });

            return id;
        }



        #endregion

        #region session log
        public static void InsertLog(int accountId, string extra)
        {
            SqlHelper.ExecuteNonQuery(dbParameters.dsnStore, "SPR_ESTORE_SessionLog_Insert", new object[] { accountId, extra });
        }
        #endregion


        #region InsertVisitor

        public static int InsertVisitor(string IpAdres)
        {
            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_UserInfo_InsertVisitor", new object[] { IpAdres });

            return id;
        }

        #endregion

        #region Update

        public static void Update(string UserName, string FirstName, string LastName, string Email, DateTime BirthDay, string Phone, string Mobile, string Fax, string PhoneInternational)
        {
            if (!string.IsNullOrEmpty(FirstName)) FirstName = FirstName.Trim();
            if (!string.IsNullOrEmpty(LastName)) LastName = LastName.Trim();
            if (!string.IsNullOrEmpty(Email)) Email = Email.Trim();

            SqlHelper.ExecuteNonQuery(dbParameters.dsnStore, "SPR_ESTORE_UserInfo_Update", new object[] { UserName, FirstName, LastName, Email, BirthDay, Phone, Mobile, Fax, PhoneInternational });
        }

        #endregion

        #region Verify

        public static int Verify(string UserName)
        {
            int i = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_UserInfo_Verify", new object[] { UserName });
            return i;
        }

        #endregion

        #region  Check Password

        public static bool CheckPassword(string Email, string Password)
        {
            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_UserPass", new object[] { Email, Crypt.CreatePasswordHash(Password) });
            if (id > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region  Change Password

        public static bool ChangePassword(string Email, string Password)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(dbParameters.dsnStore, "SPR_ESTORE_UserPassChange", new object[] { Email, Crypt.CreatePasswordHash(Password) });
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region  Reset Password

        public static string ResetPassword(string Email)
        {
            return (string)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_UserPassReset", new object[] { Email });
        }

        #endregion

        #region Guarantors

        #region GetList

        public static IList<Guarantor> GetGuarantorList(int AccountId)
        {

            IList<Guarantor> objectList = new List<Guarantor>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_User_Guarantors", objectList, new object[] { AccountId });

            return objectList;
        }
        #endregion

        #region GetDDL

        public static List<GuarantorDDL> GetGuarantorDDL(int AccountId)
        {

            List<GuarantorDDL> objectList = new List<GuarantorDDL>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Guarantor_Combo", objectList, new object[] { AccountId });

            return objectList;
        }
        #endregion

        #region GetDDL

        public static List<GuarantorDDL> GetVisitorGuarantorDDL(int AccountId)
        {

            List<GuarantorDDL> objectList = new List<GuarantorDDL>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Visitor_Guarantor_Combo", objectList, new object[] { AccountId });

            return objectList;
        }
        #endregion

        #region GetGuarantor

        public static Guarantor GetGuarantor(int AccountId)
        {

            IList<Guarantor> objectList = new List<Guarantor>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_User_Guarantor_Get", objectList, new object[] { AccountId });
            var obj = objectList.FirstOrDefault();
            if (obj != null)
            {
                obj.EvKendineAit = obj.EvKira ? 1 : 0;
            }
            return obj;
        }
        #endregion

        #region GetVisitorGuarantor

        public static Guarantor GetVisitorGuarantor(int AccountId)
        {

            IList<Guarantor> objectList = new List<Guarantor>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Visitor_Guarantor_Get", objectList, new object[] { AccountId });

            return objectList.FirstOrDefault();
        }
        #endregion

        #region InsertGuarantors

        public static int InsertGuarantors(int AccountId, Guarantor g)
        {

            bool KendineAit = g.EvKendineAit == 1 ? true : false;
            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_User_Guarantors_Insert", new object[] { AccountId, g.AccountId, g.FirstName, g.LastName, g.Cinsiyet, g.DogumTarihi, g.DogumYer,
            g.CepTel, g.EvAdres, g.EvTel, g.IsYeriAdi, g.IsAdres, g.IsTel, g.IsGorev, g.Baba_Ad, g.Anne_Ad, g.N_TCKimlikNo, g.N_SeriNo, g.N_Cuzdan_No, g.N_KutukIl, g.N_Ilce, g.N_Mahalle, g.N_CiltNo,
            g.N_AileSiraNo, g.N_SiraNo, KendineAit});
            return id;
        }
        #endregion

        #region InsertVisitorGuarantors

        public static int InsertVisitorGuarantors(int AccountId, Guarantor g)
        {

            bool KendineAit = g.EvKendineAit == 1 ? true : false;
            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Visitor_Guarantors_Insert", new object[] { AccountId, g.AccountId, g.FirstName, g.LastName, g.Cinsiyet, g.DogumTarihi, g.DogumYer,
            g.CepTel, g.EvAdres, g.EvTel, g.IsYeriAdi, g.IsAdres, g.IsTel, g.IsGorev, g.Baba_Ad, g.Anne_Ad, g.N_TCKimlikNo, g.N_SeriNo, g.N_Cuzdan_No, g.N_KutukIl, g.N_Ilce, g.N_Mahalle, g.N_CiltNo,
            g.N_AileSiraNo, g.N_SiraNo, KendineAit});
            return id;
        }
        #endregion



        #region GetUserIdentity

        public static Nufus GetUserIdentity(int AccountId)
        {

            IList<Nufus> objectList = new List<Nufus>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_User_Identity_Get", objectList, new object[] { AccountId });
            var obj = objectList.FirstOrDefault();
            if (obj != null)
            {
                obj.EvKendineAit = obj.EvKira ? 1 : 0;
            }

            return obj;
        }
        #endregion

        #region SetOrderUserIdentity

        public static int SetOrderUserIdentity(int OrderId, int AccountId, Nufus idn)
        {
            bool KendineAit = idn.EvKendineAit == 1 ? true : false;

            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Order_Identity_Insert", new object[] { OrderId, AccountId, idn.FirstName, idn.LastName, idn.Cinsiyet, idn.DogumTarihi, idn.DogumYer, idn.CepTel,
            idn.EvTel, idn.EvAdres, idn.IsYeriAdi, idn.IsAdres, idn.IsTel, idn.IsGorev, KendineAit, idn.Baba_Ad, idn.Anne_Ad, idn.N_SeriNo, idn.N_Cuzdan_No, idn.N_KutukIl, idn.N_Ilce, idn.N_Mahalle, idn.N_CiltNo,
            idn.N_AileSiraNo, idn.N_SiraNo, idn.N_TCKimlikNo, idn.AylikGelir});

            return id;
        }
        #endregion

        #region SetOrderUserGuarantor

        public static int SetOrderUserGuarantor(int OrderId, int AccountId, int GuarantorId)
        {
            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Order_Guarantor_Insert", new object[] { OrderId, AccountId, GuarantorId });
            return id;
        }

        #endregion

        #region GetOrderUserInfo

        public static OrderUserInfo GetOrderUserInfo(int OrderId)
        {
            OrderUserInfo UserInfo = new OrderUserInfo();
            IList<Nufus> objectList = new List<Nufus>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Order_Identity_Get", objectList, new object[] { OrderId });
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Order_Guarantor_List", UserInfo.Kefiller, new object[] { OrderId });
            UserInfo.Kimlik = objectList.FirstOrDefault();
            if (UserInfo.Kimlik != null)
            {
                UserInfo.Kimlik.EvKendineAit = UserInfo.Kimlik.EvKira ? 1 : 0;
            }
            return UserInfo;
        }


        #endregion


        #endregion
    }
}
